package com.kitty.game.friend.facade;


import com.kitty.common.utils.SpringUtils;
import com.kitty.game.friend.message.*;
import com.kitty.game.friend.service.FriendService;
import com.kitty.game.market.message.ReqExchangeContactSeller;
import com.kitty.game.role.model.Role;
import com.kitty.game.role.service.RoleService;
import com.kitty.mina.annotation.RequestMapping;
import com.kitty.mina.cache.SessionUtils;
import com.kitty.mina.message.MessagePusher;
import org.apache.mina.core.session.IoSession;
import org.nutz.json.Json;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;


@Controller
public class FriendController {
    Logger logger = LoggerFactory.getLogger(FriendController.class);

    @Autowired
    private RoleService roleService;
    /**
     * 增加好友分组
     */
    @RequestMapping
    public void addFriendGroup(IoSession session, ReqAddFriendGroup reqAddFriendGroup) {
        logger.info("增加好友分组=={}", Json.toJson(reqAddFriendGroup));
        SpringUtils.getBean(FriendService.class).addFriendGroup(session, reqAddFriendGroup);

    }
    @RequestMapping
    public void reqOpenGiveWindow(IoSession session, ReqOpenGiveWindow reqOpenGiveWindow) {

        Role role = SessionUtils.getRoleBySession(session);
    }
    /**
     * 向好友发起赠送
     * @param session
     * @param reqGiving
     */
    @RequestMapping
    public void reqGiving(IoSession session, ReqGiving reqGiving) {

        Role role = SessionUtils.getRoleBySession(session);
        logger.error("发起赠送：{}", reqGiving.getGid());

        Role friend = roleService.getOnlinePlayer(Long.parseLong(reqGiving.getGid()));

        logger.error("发起赠送：{}", friend.getName());


        RespGiving respGiving = new RespGiving();
        respGiving.setGiverName(role.getName());
        respGiving.setGiverLevel(role.getLevel());
        respGiving.setGiverIcon(role.getRoleIcon());
        respGiving.setReceiveName(friend.getName());
        respGiving.setReceiveLevel(friend.getLevel());
        respGiving.setReceiveIcon(friend.getRoleIcon());

        MessagePusher.pushMessage(role, respGiving);
        MessagePusher.pushMessage(friend, respGiving);

        RespOpenGivingWindow openGivingWindow = new RespOpenGivingWindow();
        openGivingWindow.setGiverName(role.getName());
        openGivingWindow.setGiverIcon(role.getRoleIcon());
        openGivingWindow.setChildType( role.getChildInfo().getType());
        openGivingWindow.setLeftTimes((byte) 1);

        openGivingWindow.setReceiverName(friend.getName());
        openGivingWindow.setReceiverIcon(friend.getRoleIcon());
        openGivingWindow.setReceiverChildType( friend.getChildInfo().getType());
        openGivingWindow.setReceiverLeftTime((byte) 1);

//        friend.setGiveRoleId(role.getRoleId());

        MessagePusher.pushMessage(role, openGivingWindow);

    }

    /**
     * 请求好友blog
     */
    @RequestMapping
    public void reqNewBlogStatus(IoSession session, ReqNewBlogStatus reqNewBlogStatus) {
        //TOO 好友blog系统

    }
    /**
     * 发送好友消息
     */
    @RequestMapping
    public void sendFriendMsg(Role role, ReqSendFriendMsg reqSendFriendMsg) {
        logger.info("发送好友消息=={}", Json.toJson(reqSendFriendMsg));
        SpringUtils.getBean(FriendService.class).sendFriendMsg(role, reqSendFriendMsg);

    }

    /**
     * 申请添加好友
     */
    @RequestMapping
    public void addFriend(Role role, ReqAddFriend reqAddFriend) {
        logger.info("申请添加好友=={}", Json.toJson(reqAddFriend));
        switch (reqAddFriend.getGroupNumber()) {
            case "1":
                /**加好友*/
                SpringUtils.getFriendService().reqAddFriend(role, reqAddFriend.getFriendName());
                break;
            case "5":
                /**加黑名单*/
                SpringUtils.getFriendService().reqAddBlack(role, reqAddFriend.getFriendName());
                break;
        }

    }

    /**
     * 发送给好友验证消息
     */
    @RequestMapping
    public void addFriend(IoSession session, ReqVerifyInfo reqVerifyInfo) {
        logger.info("发送给好友验证消息=={}", Json.toJson(reqVerifyInfo));
        SpringUtils.getBean(FriendService.class).sendVerifyInfo(session, reqVerifyInfo);
    }


    /**
     * 接受或拒绝好友请求
     */
    @RequestMapping
    public void acceptRequest(IoSession session, ReqAcceptRequest reqAcceptRequest) {
        logger.info("接受或拒绝好友请求=={}", Json.toJson(reqAcceptRequest));
        SpringUtils.getBean(FriendService.class).acceptRequest(session, reqAcceptRequest);
    }


    /**
     * 修改分组名
     */
    @RequestMapping
    public void editFriendGroupName(IoSession session, ReqEditFriendGroupName reqEditFriendGroupName) {
        logger.info("修改分组名=={}", Json.toJson(reqEditFriendGroupName));
        SpringUtils.getBean(FriendService.class).editFriendGroupName(session, reqEditFriendGroupName);

    }

    /**
     * 移动好友分组
     */
    @RequestMapping
    public void moveFriendGroup(IoSession session, ReqMoveFriendGroup reqMoveFriendGroup) {
        logger.info("移动好友分组=={}", Json.toJson(reqMoveFriendGroup));
        SpringUtils.getBean(FriendService.class).moveFriendGroup(session, reqMoveFriendGroup);

    }

    /**
     * 删除好友分组
     */
    @RequestMapping
    public void removeFriendGroup(IoSession session, ReqRemoveFriendGroup reqRemoveFriendGroup) {
        logger.info("删除好友分组=={}", Json.toJson(reqRemoveFriendGroup));
        SpringUtils.getBean(FriendService.class).removeFriendGroup(session, reqRemoveFriendGroup);
    }

    /**
     * 修改好友备注
     */
    @RequestMapping
    public void friendMark(IoSession session, ReqFriendMark reqFriendMark) {
        logger.info("修改好友备注=={}", Json.toJson(reqFriendMark));
        SpringUtils.getBean(FriendService.class).friendMark(session, reqFriendMark);
    }


    /**
     * 删除好友
     */
    @RequestMapping
    public void removeFriend(Role role, ReqRemoveFriend reqRemoveFriend) {
        logger.info("删除好友=={}", Json.toJson(reqRemoveFriend));
        switch (reqRemoveFriend.getGroupNumber()) {
            case "1":
                /**删除好友*/
                SpringUtils.getFriendService().removeFriend(role, reqRemoveFriend.getFriendName());
                break;
            case "5":
                /**删除黑名单*/
                SpringUtils.getFriendService().removeBlack(role, reqRemoveFriend.getFriendName());
                break;
        }
    }


    /**
     * 搜索好友
     */
    @RequestMapping
    public void searchFriend(IoSession session, ReqSearchFriend reqSearchFriend) {
        logger.info("搜索好友=={}", Json.toJson(reqSearchFriend));
        SpringUtils.getBean(FriendService.class).searchFriend(session, reqSearchFriend);
    }


    @RequestMapping
    public void setStrangerLevel(IoSession session, ReqStrangerLevel reqStrangerLevel) {
        logger.info("设置拒绝等级=={}", Json.toJson(reqStrangerLevel));
        SpringUtils.getBean(FriendService.class).setStrangerLevel(session, reqStrangerLevel);
    }

    /**
     * 删除好友
     */
    @RequestMapping
    public void setRefuseLevel(IoSession session, ReqRefuseLevel reqRefuseLevel) {
        logger.info("设置拒绝添加等级=={}", Json.toJson(reqRefuseLevel));
        SpringUtils.getBean(FriendService.class).setRefuseLevel(session, reqRefuseLevel);
    }

    @RequestMapping
    public void setAutoReplyMsgConfig(Role role, ReqSetAutoReplayMsgConfig reqSetAutoReplayMsgConfig) {
        SpringUtils.getFriendService().reqSetAutoReply(role, reqSetAutoReplayMsgConfig.getContent());
    }

    @RequestMapping
    public void contactSeller(Role role, ReqExchangeContactSeller reqExchangeContactSeller) {
        SpringUtils.getFriendService().contactSeller(role, reqExchangeContactSeller.getType(), reqExchangeContactSeller.getGoodsGid(), reqExchangeContactSeller.getPara());
    }

    @RequestMapping
    public void applyFriendItem(Role role, ReqApplyFriendItem reqApplyFriendItem) {
        SpringUtils.getFriendService().applyFriendItem(role, reqApplyFriendItem);
    }
}

